const express = require('express');
const router = express.Router();
const retUtil = require('../util/retUtil');
const activityUtil = require('./routerUtil/activityRouterUtil');
const activityService = require('../../service/activityService');

router.get('/',function (req, res, next) {
    res.send('respond with a 201 resource');
});

router.use(function (req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    next();
});


/*
* 201-获取活动列表
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: states
* 参数: pageNum
* 参数: pageSize
* 参数: [userId]
* 参数: [filterUser]
* 参数: [checkReward]
* 参数: [withTotalPage]
* 参数: [orgId]
*
* 接口文档地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3862
* */
function getActivityList(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const states = retUtil.arrayFromReq(req, 'states');
    const pageNum = retUtil.numberFromReq(req, 'pageNum');
    const pageSize = retUtil.numberFromReq(req, 'pageSize') || 12;

    const userId = retUtil.numberFromReq(req, 'userId')||0;
    const filterUser = retUtil.numberFromReq(req, 'filterUser')||0;
    const checkReward = retUtil.numberFromReq(req, 'checkReward')||0;
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage')||0;

    if(!states || states.length <=0) {
        return retUtil.ret_err_json('states 错误', res);
    }
    if(pageNum < 0) {
        return retUtil.ret_err_json('pageNum 错误', res);
    }
    if(filterUser || checkReward) {
        if(!userId || userId <=0) {
            return retUtil.ret_err_json('filterUser checkReward 依赖 userId', res);
        }
    }
    const opt = {
        device: device,
        type: 10,       // 标记挑战活动
        states: states,
        pageNum: pageNum,
        pageSize: pageSize,
        userId: userId,
        filterUser: filterUser,
        checkReward: checkReward,
        withTotalPage: withTotalPage,
        publish: 1, // 标记活动需要发布
    };
    activityService.getActivityList(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    })
}
router.get('/getActivityList', getActivityList);
router.post('/getActivityList', getActivityList);

/*
* 201-参与活动提交作品
*
* @author 王俊
* @date 2018-07-18
*
* 参数: device
* 参数: activityId
* 参数: userId
* 参数: url
* 参数: [desc]
* 参数: [couponCode]
*
* 接口文档地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3868
* */
function submitProductToAct(req, res, next) {
    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const activityId = retUtil.numberFromReq(req, 'activityId');
    const url = retUtil.stringFromReq(req, 'url');
    const desc = retUtil.stringFromReq(req, 'desc');
    const couponCode = retUtil.stringFromReq(req, 'couponCode');

    if(!activityId || activityId <=0) {
        return retUtil.ret_err_json('activityId 错误', res);
    }
    if(!userId || userId <=0) {
        return retUtil.ret_err_json('activityId 错误', res);
    }
    if(!url || url.indexOf('http')!==0) {
        return retUtil.ret_err_json('url 错误', res);
    }
    const opt = {
        device: device,
        userId: userId,
        activityId: activityId,
        url: url,
        desc: desc,
        couponCode: couponCode
    };
    activityService.submitProductToAct(opt, function (error, data) {
        if(error) return retUtil.ret_err_json(error, res);
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/submitProductToAct', submitProductToAct);
router.post('/submitProductToAct', submitProductToAct);

/*
* 201-获取活动详情
*
* @author 毛凯
* @date 2018-07-19
*
* 参数: device
* 参数: activityId
* 参数: topLimit
* 参数: withPromote
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3865
*
* */
function getActivityDetail(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const activityId = retUtil.numberFromReq(req, 'activityId');
    const topLimit = retUtil.numberFromReq(req, 'topLimit') || 3;
    const withPromote = retUtil.numberFromReq(req, 'withPromote') || 0;


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    if(!activityId || activityId <=0) {
        return retUtil.ret_err_json('activityId 错误', res);
    }

    const opt = {
        device: device,
        activityId: activityId,
        topLimit: topLimit,
        withPromote: withPromote
    };

    activityService.getActivityDetail(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityDetail', getActivityDetail);
router.post('/getActivityDetail', getActivityDetail);


/*
* 201-获取活动的推广信息
*
* @author 毛凯
* @date 2018-07-19
*
* 参数: device
* 参数: actId
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3871
*
* */
function getActivityPromoteInfo(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const actId = retUtil.numberFromReq(req, 'actId');


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    if(!actId || actId <=0) {
        return retUtil.ret_err_json('actId 错误', res);
    }

    const opt = {
        device: device,
        actId: actId
    };

    activityService.getActivityPromoteInfo(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityPromoteInfo', getActivityPromoteInfo);
router.post('/getActivityPromoteInfo', getActivityPromoteInfo);




/*
* 201-获取活动作品列表(该接口只提供冲顶大赛活动的作品)
*
* @author 毛凯
* @date 2018-07-20
*
* 参数: device
* 参数: actId
* 参数: pageNum
* 参数: pageSize
* 参数: withTotalPage
* 参数: states
* 参数: userId
* 参数: orderBy
*
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3886
*
* */
function getActivityProductList(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const actId = retUtil.arrayFromReq(req, 'actId') || [];
    const pageNum = retUtil.numberFromReq(req, 'pageNum');
    const pageSize = retUtil.numberFromReq(req, 'pageSize');
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage') || 0;
    const states = retUtil.arrayFromReq(req, 'states') || [10,20,30,40];
    const userId = retUtil.numberFromReq(req, 'userId') || 0;
    const orderBy = retUtil.numberFromReq(req, 'orderBy') ||0 ;


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }


    const opt = {
        device: device,
        actId: actId,
        pageNum : pageNum,
        pageSize :pageSize,
        withTotalPage : withTotalPage,
        states : states,
        userId : userId,
        orderBy : orderBy
    };

    activityService.getActivityProductList(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActivityProductList', getActivityProductList);
router.post('/getActivityProductList', getActivityProductList);



/*
* 201-获取被推荐用户列表
*
* @author 毛凯
* @date 2018-07-20
*
* 参数: device
* 参数: userId
* 参数: pageNum
* 参数: pageSize
* 参数: withTotalPage
*
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3892
*
* */
function getPromtedUserList(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const pageNum = retUtil.numberFromReq(req, 'pageNum');
    const pageSize = retUtil.numberFromReq(req, 'pageSize');
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage') || 0;


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }
    if(!userId || userId <= 0) {
        return retUtil.ret_err_json('userId 错误', res);
    }


    const opt = {
        device: device,
        userId: userId,
        pageNum : pageNum,
        pageSize :pageSize,
        withTotalPage : withTotalPage
    };

    activityService.getPromtedUserList(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getPromtedUserList', getPromtedUserList);
router.post('/getPromtedUserList', getPromtedUserList);


/*
* 201-获取活动推荐方排行榜
*
* @author 毛凯
* @date 2018-07-20
*
* 参数: device
* 参数: userId
* 参数：actId
* 参数：order
* 参数：privacy
* 参数: pageNum
* 参数: pageSize
* 参数: withTotalPage
*
*
* 接口地址: http://yapi.yijianxiu.com:7000/project/182/interface/api/3892
*
* */
function getActPromoterRankingList(req, res, next) {

    const device = retUtil.numberFromReq(req, 'device');
    const userId = retUtil.numberFromReq(req, 'userId');
    const actId = retUtil.numberFromReq(req, 'actId');
    const order = retUtil.numberFromReq(req, 'order');
    const privacy = retUtil.numberFromReq(req, 'privacy');
    const pageNum = retUtil.numberFromReq(req, 'pageNum') || 0;
    const pageSize = retUtil.numberFromReq(req, 'pageSize');
    const withTotalPage = retUtil.numberFromReq(req, 'withTotalPage') || 0;


    if(!device || device <700) {
        return retUtil.ret_err_json('device 错误', res);
    }


    const opt = {
        device: device,
        userId: userId,
        actId :actId,
        order :order,
        privacy : privacy,
        pageNum : pageNum,
        pageSize :pageSize,
        withTotalPage : withTotalPage
    };

    activityService.getActPromoterRankingList(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/getActPromoterRankingList', getActPromoterRankingList);
router.post('/getActPromoterRankingList', getActPromoterRankingList);



//==========================================================================
module.exports = router;